package com.cn.serverline.lineicp.productionLine.mapper;

import com.cn.serverline.lineicp.productionLine.entity.ProductMix;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 产品结构表 Mapper 接口
 * </p>
 *
 * @author Gmr
 * @since 2024-07-30
 */
public interface ProductMixMapper extends BaseMapper<ProductMix> {

	/**
	 * 根据id获取指定节点及子节点
	 *
	 * @author 郭明然
	 * @since 2024/7/15 15:45
	 * @param id 对象id
	 * @return List<ProductMix> 指定节点及子节点
	 */
	@Select("WITH RECURSIVE cte AS(" +
			"SELECT " +
			"PRODUCT_MIX.ID," +
			"PRODUCT_MIX.PARENT_ID," +
			"PRODUCT_MIX.NAME," +
			"PRODUCT_MIX.CATEGORY," +
			"PRODUCT_MIX.COMPONENT_CODE," +
			"PRODUCT_MIX.PROCESS_STAGE," +
			"PRODUCT_MIX.PRODUCT_CODE," +
			"PRODUCT_MIX.MAIN_MANUFACTURING_PLANT," +
			"PRODUCT_MIX.STAGE_MARKING," +
			"PRODUCT_MIX.EDITION," +
			"PRODUCT_MIX.CREATE_TIME," +
			"PRODUCT_MIX.CREATE_USER," +
			"PRODUCT_MIX.UPDATE_TIME," +
			"PRODUCT_MIX.UPDATE_USER \n" +
			"FROM PRODUCT_MIX \n" +
			"WHERE PRODUCT_MIX.ID = #{id} \n" +
			"UNION ALL \n" +
			"SELECT " +
			"child.ID," +
			"child.PARENT_ID," +
			"child.NAME," +
			"child.CATEGORY," +
			"child.COMPONENT_CODE," +
			"child.PROCESS_STAGE," +
			"child.PRODUCT_CODE," +
			"child.MAIN_MANUFACTURING_PLANT," +
			"child.STAGE_MARKING," +
			"child.EDITION," +
			"child.CREATE_TIME," +
			"child.CREATE_USER," +
			"child.UPDATE_TIME," +
			"child.UPDATE_USER \n" +
			"FROM CT_PRODUCT_MIX child \n" +
			"INNER JOIN cte parent on child.PARENT_ID = parent.ID" +
			")" +
			"SELECT * FROM cte"
	)
	public List<ProductMix> getTreeById(Long id);

}
